{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.preprocessing import scale"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## sklearn实现支持向量机"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X.shape=(1000, 41),Y.shape=(1000,)\n"
     ]
    }
   ],
   "source": [
    "#数据导入及数据预处理\n",
    "telco=np.array(pd.read_csv('telco.csv',header=0).fillna(0))#读入文件，并将所有的缺省值变为0\n",
    "X=scale(telco[:,:-1])#取前41列作为特征并进行归一化操作\n",
    "Y=telco[:,-1]\n",
    "print(f'X.shape={X.shape},Y.shape={Y.shape}')\n",
    "#数据集划分\n",
    "x_train, x_test, y_train, y_test = \\\n",
    "            train_test_split(X, Y, test_size=0.3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率为：0.7833333333333333\n"
     ]
    }
   ],
   "source": [
    "#构建模型\n",
    "svc_model = SVC(kernel='rbf')\n",
    "svc_model.fit(x_train,y_train)#将训练数据放入模型进行训练\n",
    "sorce = svc_model.score(x_test,y_test)  # 使用测试数据和标签对模型进行评估，得到模型预测的准确率\n",
    "print(f'准确率为：{sorce}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
